// Created by Roman Novitsky on 22.04.2009


#region Imported Namespaces


using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

using WtfManager.Framework;


#endregion


namespace WtfManager.HierarchicalData
{
  [ValueConversion(typeof(TreeItem), typeof(ImageSource))]
  public class TreeItemToImageConverter: ExtensibleService, IValueConverter
  {
    #region Fields


    private static TreeItemToImageConverter _instance;


    #endregion


    #region Constructors/Finalizers


    internal TreeItemToImageConverter()
    {
    }


    #endregion


    #region Properties/Indexers


    public override string Description
    {
      get
      {
        return "Converts Tree Items to images.";
      }
    }

    public static TreeItemToImageConverter Instance
    {
      get
      {
        return _instance;
      }
    }


    public override string Name
    {
      get
      {
        return "Tree Item To Image Converter";
      }
    }


    #endregion


    #region Methods


    protected override void OnStateChnaged()
    {
      _instance = IsInitialized ? this : null;
    }


    protected override FrameworkComponentState OnLoad()
    {
      return FrameworkComponentState.Loaded;
    }


    protected override void OnUnload()
    {
      _instance = null;
    }


    protected override FrameworkComponentState OnInitialize()
    {
      return FrameworkComponentState.Initialized;
    }


    protected override void OnExtensionAdded(Service extension)
    {
    }


    public override bool IsExtension(Service extension)
    {
      return extension is ITreeItemToImageConverterExtension;
    }


    protected override void OnExtensionRemoved(Service extension)
    {
    }


    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      Debug.Assert(IsInitialized, "TreeItemToImageConverter is not yet initialized.");

      var treeItem = value as TreeItem;

      if (treeItem == null)
      {
        return null;
      }

      lock (this)
      {
        bool largeIcon = GetParameterValue(parameter);

        foreach (ITreeItemToImageConverterExtension extension in Extensions)
        {
          try
          {
            ImageSource imageSource = extension.GetImage(treeItem, largeIcon);

            if (imageSource != null)
            {
              return imageSource;
            }
          }
          catch
          {
            continue;
          }
        }
      }

      return null;
    }


    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new NotSupportedException();
    }


    private static bool GetParameterValue(object parameter)
    {
      if (parameter is string)
      {
        bool result;
        bool.TryParse((string)parameter, out result);

        return result;
      }

      return parameter is bool ? (bool)parameter : false;
    }


    #endregion
  }
}